// ----------------------------
//     // // /    //   // // //
//    //         //   //
//   // //      //   // // //
//        //   //   //      //
//       //   //    //      //
// // //     //      // // //
// ----------------------------
// -       sidebarView        -
// ----------------------------
(function(ב) {
    // 开启严格模式
    "use strict";
    
    // 获取最外层
    $.ajax({
        url: 'http://www.ynpasc.cn/onemap/services/tree/requestAllLayersByFid',
        ype: "post",
        data: {fid: 0},
        dataType: "json",
        async: false,
        success: function(result1){
            $.ajax({
                url: 'http://www.ynpasc.cn/onemap/services/OnemapElementController/getAllOnemapElement',
                ype: "post",
                data: {},
                dataType: "json",
                async: false,
                success: function(result2){
                    let resultAll = result1['data'].concat(result2['data']);
                    recursive($(".tree ul"), formatConversion(resultAll), 1);
                }
            });
        }
    });

    // 数据格式转换
    function formatConversion(resultAll) {
        // 根据关键之剔除数据
        // 第一次剔除
        let firstRemove = [];
        for (let x = 0; x < resultAll.length; x++) {
            if (resultAll[x]['lLevel'] != 0 &&
                resultAll[x]['lLevel'] != undefined) {

                let structure = {};
                structure['id'] = resultAll[x]['id'];
                structure['pid'] = resultAll[x]['layerFid'];
                structure['name'] = resultAll[x]['layerName'];
                structure['children'] = [];
                firstRemove.push(structure);

            }
        }

        // 第一次合并
        let firstMerge = [];
        for (let x = 0; x < firstRemove.length; x++) {
            for (let y = 0; y < firstRemove.length; y++) {
                if (firstRemove[y]['pid'] == firstRemove[x]['id']) {
                    firstMerge.push(x);
                    firstRemove[x]['children'].push(firstRemove[y]);
                }
            }
        }
        firstMerge = ב.toHeavy(firstMerge);

        // 第二次剔除
        let secondRemove = [];
        for (let x = 0; x < firstMerge.length; x++) {
            secondRemove.push(firstRemove[firstMerge[x]]);
        }

        // 第三次剔除 - (特殊处理需求渲染的类别)
        let thirdRemove = [];
        for (let x = 0; x < resultAll.length; x++) {
            if (resultAll[x]['lLevel'] == undefined) {

                let structure = {};
                structure['id'] = resultAll[x]['elementCode'];
                structure['pid'] = resultAll[x]['layerId'];
                structure['name'] = resultAll[x]['elementName'];
                structure['children'] = [];

                let childNodes = resultAll[x]['childrenNode'];
                for (let y = 0; y < childNodes.length; y++) {
                    let structurex = {};
                    structurex['id'] = childNodes[y]['elementCode'];
                    structurex['pid'] = childNodes[y]['parentId'];
                    structurex['name'] = childNodes[y]['elementName'];
                    structurex['type'] = "time";
                    structurex['date'] = childNodes[y]['elementCode'];
                    structurex['path'] = childNodes[y]['url'];
                    structurex['children'] = [];

                    structure['children'].push(structurex);
                }

                thirdRemove.push(structure);
            }
        }

        // 第二次合并
        let secondMerge = [];
        for (let x = 0; x < thirdRemove.length; x++) {
            for (let y = 0; y < secondRemove.length; y++) {
                for (let z = 0; z < secondRemove[y]['children'].length; z++) {
                    if (thirdRemove[x]['pid'] == secondRemove[y]['children'][z]['id']) {
                        secondRemove[y]['children'][z]['children'].push(thirdRemove[x]);
                        break;
                    }
                }
            }
        }
        secondMerge = secondRemove;

        // 追加特殊要素子项 - 气象灾害监测预警
        let addChildElmt = [{
            "id": "yunyan",
            "pid": "202",
            "name": "云烟",
            "children": [{
                "id": "xiayan",
                "pid": "yunyan",
                "name": "夏烟",
                "children": [{
                    "id": "xytjbgsdj",
                    "pid": "xiayan",
                    "name": "炭疽病",
                    "children": [{
                        "id": "ri",
                        "pid": "xytjbgsdj",
                        "name": "日",
                        "date": "ri",
                        "type": "time",
                        "path": "xytjbgsdj/ri/date_yunnan_yunyan_xiayan_3_xytjbgsdj_ri.json",
                        "children": []
                    }]
                }, {
                    "id": "xyyhbfsdj",
                    "pid": "xiayan",
                    "name": "野火病",
                    "children": [{
                        "id": "ri",
                        "pid": "xyyhbfsdj",
                        "name": "日",
                        "date": "ri",
                        "type": "time",
                        "path": "xyyhbfsdj/ri/date_yunnan_yunyan_xiayan_3_xyyhbfsdj_ri.json",
                        "children": []
                    }]
                }, {
                    "id": "xyghdj",
                    "pid": "xiayan",
                    "name": "干旱",
                    "children": [{
                        "id": "ri",
                        "pid": "xyghdj",
                        "name": "日",
                        "date": "ri",
                        "type": "time",
                        "path": "xyghdj/ri/date_yunnan_yunyan_xiayan_3_xyghdj_ri.json",
                        "children": []
                    }]
                }, {
                    "id": "xyhjbfsdj",
                    "pid": "xiayan",
                    "name": "黑胫病",
                    "children": [{
                        "id": "ri",
                        "pid": "xyhjbfsdj",
                        "name": "日",
                        "date": "ri",
                        "type": "time",
                        "path": "xyhjbfsdj/ri/date_yunnan_yunyan_xiayan_3_xyhjbfsdj_ri.json",
                        "children": []
                    }]
                }, {
                    "id": "xyghfbfsdj",
                    "pid": "xiayan",
                    "name": "根黑腐病",
                    "children": [{
                        "id": "ri",
                        "pid": "xyghfbfsdj",
                        "name": "日",
                        "date": "ri",
                        "type": "time",
                        "path": "xyghfbfsdj/ri/date_yunnan_yunyan_xiayan_3_xyghfbfsdj_ri.json",
                        "children": []
                    }]
                }]
            }]
        }];
        secondMerge[1]["children"][0]["children"] = addChildElmt;
        console.log(secondMerge);
        return secondMerge;
    }

    // 递归实例化树目录
    function recursive(that, param, num) {
        for (let x = 0; x < param.length; x++) {
            
            if (param[x]['children'] == "") {
                let item = "<li class=\"layer layer_"+ num +"\" eid=\""+ param[x]['id'] +"\" pid=\""+ param[x]['pid'] +"\" type=\""+ param[x]['type'] +"\">"
                            +"<h class=\"layer_title last\" type=\""+ param[x]['type'] +"\" date=\""+ param[x]['date'] +"\" path=\""+ param[x]['path'] +"\">"+ param[x]['name'] +"</h>"
                            +"<ul class=\"layer_content\"></ul></li>";
                that.append(item);

                continue;
            } else {
                let item = "<li class=\"layer layer_"+ num +"\" eid=\""+ param[x]['id'] +"\" pid=\""+ param[x]['pid'] +"\">"
                            +"<h class=\"layer_title\">"+ param[x]['name'] +"<i class=\"fa fa-angle-down\" aria-hidden=\"true\"></i></h>"
                            +"<ul class=\"layer_content\"></ul></li>";
                that.append(item);

                recursive(that.find("> li").eq(x).find("> ul"), param[x]['children'], num+1);
            }
        }
    }
})(onemap);